<?php View::display('accounting/menu');?>
<!-- .head -->
<header class="head">
	<!-- ."main-bar -->
	<div class="main-bar">
		<div class="container-fluid">
			<div class="row-fluid">
				<div class="span12">
					<h3>
						<i class="icon-barcode"></i> Invoice
					</h3>
				</div>
			</div>
			<!-- /.row-fluid -->
		</div>
		<!-- /.container-fluid -->
	</div>
	<!-- /.main-bar -->
</header>
<!-- /.head -->
<!-- #content -->
<div id="content">
	<!-- .outer -->
	<div class="container-fluid outer">
	
		<div class="row-fluid">
			<!-- .inner -->
			<div class="span12 inner">
				<!--Begin Datatables-->
				<div class="row-fluid">
					<div class="span12">
						<div class="box">
							<div class="body">
								เลือกดูของ
								<select class="selectShow">
									<?php $typeShow = isset($_GET['mode'])?$_GET['mode']:'daily' ;?>
									<option value="index.php?page=accounting/invoice&mode=daily" <?php echo $typeShow=='daily'? 'selected': ''; ?>>วันนี้</option>
									<option value="index.php?page=accounting/invoice&mode=monthly" <?php echo $typeShow=='monthly'? 'selected': ''; ?>>เดือนนี้</option>
									<option value="index.php?page=accounting/invoice&mode=yearly" <?php echo $typeShow=='yearly'? 'selected': ''; ?>>ปีนี้</option>
								</select>
								<!-- 
								<a href="index.php?page=accounting/invoice&mode=daily">วันนี้</a>
								<a href="index.php?page=accounting/invoice&mode=weekly">สัปดาห์นี้</a>
								<a href="index.php?page=accounting/invoice&mode=monthly">เดือนนี้</a>
								<a href="index.php?page=accounting/invoice&mode=yearly">ปีนี้</a>
								 -->
							</div>
						</div>
						<div class="box">
							<header>
								<h5>ใบสั่งซื้อที่ยังไม่ได้จ่าย</h5>
							</header>
							<div class="body">
								<table class="table table-bordered dataTable responsive">
									<thead>
										<tr>
											<th class="header">วันที่สั่งซื้อ</th>
                                            <td class="header">ประเภท</td>
											<th class="header">ผู้ใช้</th>
											<th class="header">บริการ</th>
											<th class="header">ค่าใช้จ่าย</th>
											<th class="header">กำหนดชำระ</th>
											<th class="header"></th>
										</tr>
									</thead>
									<tbody>
										<?php
										foreach($params['invoices'] as $key => $row){
											//$paidButton = $row['status'] == 0? '<button class="paidButton btn btn-success" aid="'.$row['id'].'">เปลี่ยนเป็นจ่ายแล้ว</button>': '';
											$info = nl2br($row['info']);
											$price = Helper::formatMoney($row['price']);
											$dateDue = new DateTime($row['dateDue']);
											$dateDue = $dateDue->format('j/m/Y');

											$createTime = new DateTime($row['createTime']);

											if($createTime->format('Y-m-d') == date('Y-m-d')) $type = 'Daily';
											else if($createTime->format('Y-m') == date('Y-m')) $type = 'Monthly';
											else if($createTime->format('Y') == date('Y')) $type = 'Yearly';
											else if($type) 'Previous year';
											//$type = isset($_GET['mode'])? $_GET['mode']: 'daily';

											if($row['status'] != 1){
												$paidNewButton = '<li><a class="paidTechnic" href="#">จ่ายแล้ว+แจ้งติดตั้ง</a></li>';
											}
											else {
												$paidNewButton = '';
											}

											$buyDate = new DateTime($row['createTime']);
											$buyDate = $buyDate->format('j/n/Y');

                                            $type = $row['isNew']=='1'? 'สั่งซื้อ': 'ต่ออาย';

											echo <<<HTML
											<tr class="list-row" rowId="{$row['id']}">
												<td class="createTime">{$buyDate}</td>
												<td class="type">{$type}</td>
												<td class="email">
													<a target="_blank" href="old.php?page=accounting/account/detail&id={$row['account']['id']}">
														{$row['account']['email']}
													</td>
												<td class="package">{$row['package']['type']} - {$row['package']['name']}</td>
												<td class="price">{$price} บาท</td>
												<td class="expire">{$dateDue}</td>
												<td>
												<!--
													<div class="btn-group">
														<a class="btn dropdown-toggle action" data-toggle="dropdown" href="#">
															Action
															<span class="caret"></span>
														</a>
														<ul class="dropdown-menu">
															<li><a class="paid" href="#">จ่ายแล้ว</a></li>
															{$paidNewButton}
															<li class="divider"></li>
															<li><a target="_blank" href="old.php?page=invoice&id={$row['id']}">รายละเอียด</a></li>
														</ul>
													</div>
                                                -->
                                                    <a class="btn" target="_blank" href="old.php?page=invoice&id={$row['id']}">รายละเอียด</a>
												</td>
											</tr>
HTML;
										}?>
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
				<div class="row-fluid">
					<div class="span12">
						<div class="box">
							<header>
								<h5>ใบสั่งซื้อที่จ่ายแล้ว</h5>
							</header>
							<div class="body">
								<table class="table table-bordered dataTable responsive">
									<thead>
										<tr>
											<th class="header">วันที่สั่งซื้อ</th>
                                            <td class="header">ประเภท</td>
											<th class="header">ผู้ใช้</th>
											<th class="header">บริการ</th>
											<th class="header">ค่าใช้จ่าย</th>
											<th class="header">กำหนดชำระ</th>
											<th class="header"></th>
										</tr>
									</thead>
									<tbody>
										<?php
										foreach($params['invoicesPay'] as $key => $row){
											//$paidButton = $row['status'] == 0? '<button class="paidButton btn btn-success" aid="'.$row['id'].'">เปลี่ยนเป็นจ่ายแล้ว</button>': '';
											$info = nl2br($row['info']);
											$price = Helper::formatMoney($row['price']);
											$dateDue = new DateTime($row['dateDue']);
											$dateDue = $dateDue->format('j/m/Y');

											$createTime = new DateTime($row['createTime']);

											if($createTime->format('Y-m-d') == date('Y-m-d')) $type = 'Daily';
											else if($createTime->format('Y-m') == date('Y-m')) $type = 'Monthly';
											else if($createTime->format('Y') == date('Y')) $type = 'Yearly';
											else if($type) 'Previous year';
											//$type = isset($_GET['mode'])? $_GET['mode']: 'daily';

											$buyDate = new DateTime($row['createTime']);
											$buyDate = $buyDate->format('j/n/Y');

                                            $type = $row['isNew']=='1'? 'สั่งซื้อ': 'ต่ออายุ';

											echo <<<HTML
											<tr class="list-row" rowId="{$row['id']}">
												<td class="createTime">{$buyDate}</td>
												<td class="type">{$type}</td>
												<td class="email">
													<a target="_blank" href="old.php?page=accounting/account/detail&id={$row['account']['id']}">
														{$row['account']['email']}
													</td>
												<td class="package">{$row['package']['type']} - {$row['package']['name']}</td>
												<td class="price">{$price} บาท</td>
												<td class="expire">{$dateDue}</td>
												<td>
													<a class="btn" target="_blank" href="old.php?page=invoice&id={$row['id']}">รายละเอียด</a>
												</td>
											</tr>
HTML;
										}?>
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<!--Begin Datatables-->
	</div>
</div>
<script type="text/template" class="modalPaidNew">
	<div class="modal hide fade">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
			<h3>ยืนยันการจ่ายเงิน + สร้าง product (ลูกค้าใหม่)</h3>
		</div>
		<div class="modal-body">
			<form>
				<label>package</label>
				<div class="alert alert-info package"></div>
				<label>account</label>
				<div class="alert alert-info email"></div>
				<label>month</label>
				<div class="alert alert-info month"></div>
				<label>price</label>
				<div class="alert alert-info price"></div>
				<div class="optional-product"></div>
				<label>info</label>
				<textarea class="info"></textarea>
			</form>
		</div>
		<div class="modal-footer">
			<a href="#" class="btn cancel">Cancel</a>
			<a href="#" class="btn btn-primary paidNew">Paid new</a>
		</div>
	</div>
</script>
<script type="text/javascript">
$(function(){
	$('.selectShow').change(function(event){
		href = $(this).val();
		location.href = href;
	});
	
	$('.list-row')
    .delegate('.delButton', 'click', function(event){
        if(!window.confirm('ยืนยันการลบ')){
            return false;
        }
		var dTarget = $(event.delegateTarget);
		var id = dTarget.attr('rowId');
		$('.deltButton', dTarget).button('loading');
		$.ajax('old.php?page=accounting/invoice.rest', {
			type: 'DELETE',
			data: {id: id},
			dataType: 'JSON',
			success: function(data){
				if(data.success){
					delRow(dTarget);
				}
                $('.deltButton', dTarget).button('reset');
			}
		});
	})
	.delegate('.paid', 'click', function(event){
		var ans = window.confirm('ตรวจสอบการจ่ายเงินแล้ว?');
		if(ans==true){
			var dTarget = $(event.delegateTarget);
			var aButton = $('.action', dTarget);
			
			var id = dTarget.attr('rowId');
			
			var sData = {id: id, status: 1};
			aButton.button('loading');

			$.ajax('old.php?page=accounting/invoice.rest', {
				type: 'OPTIONS',
				data: { action: 'paid', data: sData },
				dataType: 'JSON',
				success: function(data){
					aButton.button('reset');
					if(data.success){
						delRow(dTarget);
					}
				}
			});
		}
	})
	.delegate('.paidTechnic', 'click', function(event){
		var ans = window.confirm('ตรวจสอบการจ่ายเงินแล้ว?');
		if(ans==true){
			var dTarget = $(event.delegateTarget);
			var aButton = $('.action', dTarget);
			
			var id = dTarget.attr('rowId');
			
			var sData = {id: id, status: 1};
			aButton.button('loading');

			$.ajax('old.php?page=accounting/invoice.rest', {
				type: 'OPTIONS',
				data: { action: 'paidTechnic', data: sData },
				dataType: 'JSON',
				success: function(data){
					aButton.button('reset');
					if(data.success){
						delRow(dTarget);
					}
				}
			});
		}
	});


    var oTable = $('.dataTable').dataTable({"bFilter": false, "bInfo": false, "bSort": false,
        sDom: "<'pull-right'l>t<'row-fluid'<'span6'f><'span6'p>>",
        sPaginationType: "bootstrap",
        oLanguage: {
            "sLengthMenu": "Show _MENU_ entries"
        },
        //"iDisplayLength": 10,
        //"aLengthMenu": [[10, 25, 50], [10, 25, 50]]
    });

    function delRow(row){
        $(row).fadeOut(function(){
            row = $(row).get(0);
            oTable.fnDeleteRow(oTable.fnGetPosition(row));
        });
    }
});
</script>